4 class MapTextEditorController: NSViewController {
6 @Binding var text: String
7 let onChange: () -> Void
9 private let vertexRegex = MapParsingPatterns.vertex
10 private let edgeRegex = MapParsingPatterns.edge
11 private let blockerRegex = MapParsingPatterns.blocker
12 private let opportunityRegex = MapParsingPatterns.opportunity
13 private let noteRegex = MapParsingPatterns.note
14 private let stageRegex = MapParsingPatterns.stage
16 private let changeDebouncer: Debouncer = Debouncer(seconds: 1)
18 init(text: Binding<String>, onChange: @escaping () -> Void) {
20 self.onChange = onChange
21 super.init(nibName: nil, bundle: nil)
24 required init?(coder: NSCoder) {
25 fatalError("init(coder:) has not been implemented")
28 override func loadView() {
29 let scrollView = NSTextView.scrollableTextView()
30 let textView = scrollView.documentView as! NSTextView
32 scrollView.translatesAutoresizingMaskIntoConstraints = false
34 textView.allowsUndo = true
35 textView.delegate = self
36 textView.textStorage?.delegate = self
37 textView.string = self.text
38 textView.isEditable = true
39 textView.font = .monospacedSystemFont(ofSize: 16.0, weight: .regular)
40 self.view = scrollView
43 override func viewDidAppear() {
44 self.view.window?.makeFirstResponder(self.view)
48 extension MapTextEditorController: NSTextViewDelegate {
50 func textDidChange(_ obj: Notification) {
51 if let textField = obj.object as? NSTextView {
52 self.text = textField.string
55 changeDebouncer.debounce {
56 DispatchQueue.main.async {
63 func textView(_ view: NSTextView, shouldChangeTextIn: NSRange, replacementString: String?) -> Bool
65 let range = Range(shouldChangeTextIn, in: view.string)
66 let target = view.string[range!]
76 extension MapTextEditorController: NSTextStorageDelegate {
78 override func textStorageDidProcessEditing(_ obj: Notification) {
79 if let textStorage = obj.object as? NSTextStorage {
80 self.colorizeText(textStorage: textStorage)
84 private func colorizeText(textStorage: NSTextStorage) {
85 let range = NSMakeRange(0, textStorage.length)
86 var matches = vertexRegex.matches(in: textStorage.string, options: [], range: range)
88 for match in matches {
89 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
90 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
91 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
92 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 4))
95 matches = edgeRegex.matches(in: textStorage.string, options: [], range: range)
97 for match in matches {
98 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 1))
99 let arrowRange = match.range(at: 2)
100 textStorage.addAttributes(
101 [.foregroundColor: NSColor.syntax.symbol],
102 range: NSMakeRange(arrowRange.lowerBound - 1, arrowRange.length + 1))
103 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 3))
106 matches = opportunityRegex.matches(in: textStorage.string, options: [], range: range)
108 for match in matches {
109 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
110 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
111 textStorage.addAttributes([.foregroundColor: NSColor.syntax.symbol], range: match.range(at: 3))
112 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 4))
115 matches = blockerRegex.matches(in: textStorage.string, options: [], range: range)
117 for match in matches {
118 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
119 textStorage.addAttributes([.foregroundColor: NSColor.syntax.vertex], range: match.range(at: 2))
122 matches = noteRegex.matches(in: textStorage.string, options: [], range: range)
124 for match in matches {
125 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
126 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
127 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 3))
130 matches = stageRegex.matches(in: textStorage.string, options: [], range: range)
132 for match in matches {
133 textStorage.addAttributes([.foregroundColor: NSColor.syntax.option], range: match.range(at: 1))
134 textStorage.addAttributes([.foregroundColor: NSColor.syntax.number], range: match.range(at: 2))
139 struct MapTextEditor: NSViewControllerRepresentable {
141 @Binding var text: String
142 var onChange: () -> Void = {}
144 func makeNSViewController(
145 context: NSViewControllerRepresentableContext<MapTextEditor>
146 ) -> MapTextEditorController {
147 return MapTextEditorController(text: $text, onChange: onChange)
150 func updateNSViewController(
151 _ nsViewController: MapTextEditorController,
152 context: NSViewControllerRepresentableContext<MapTextEditor>